package org.dromara.event.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.event.domain.vo.ChatMessageVo;
import org.dromara.event.domain.bo.ChatMessageBo;
import org.dromara.event.service.IChatMessageService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 聊天消息（按群组分区存储）
 *
 * @author 吴俊杰
 * @date 2025-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/event/message")
public class ChatMessageController extends BaseController {

    private final IChatMessageService chatMessageService;

    /**
     * 查询聊天消息（按群组分区存储）列表
     */
    @SaCheckPermission("event:message:list")
    @GetMapping("/list")
    public TableDataInfo<ChatMessageVo> list(ChatMessageBo bo, PageQuery pageQuery) {
        return chatMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聊天消息（按群组分区存储）列表
     */
    @SaCheckPermission("event:message:export")
    @Log(title = "聊天消息（按群组分区存储）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChatMessageBo bo, HttpServletResponse response) {
        List<ChatMessageVo> list = chatMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "聊天消息（按群组分区存储）", ChatMessageVo.class, response);
    }

    /**
     * 获取聊天消息（按群组分区存储）详细信息
     *
     * @param messageId 主键
     */
    @SaCheckPermission("event:message:query")
    @GetMapping("/{messageId}")
    public R<ChatMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long messageId) {
        return R.ok(chatMessageService.queryById(messageId));
    }

    /**
     * 新增聊天消息（按群组分区存储）
     */
    //@SaCheckPermission("event:message:add")
    @Log(title = "聊天消息（按群组分区存储）", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ChatMessageBo bo) {
        return toAjax(chatMessageService.insertByBo(bo));
    }

    /**
     * 修改聊天消息（按群组分区存储）
     */
    @SaCheckPermission("event:message:edit")
    @Log(title = "聊天消息（按群组分区存储）", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChatMessageBo bo) {
        return toAjax(chatMessageService.updateByBo(bo));
    }

    /**
     * 删除聊天消息（按群组分区存储）
     *
     * @param messageIds 主键串
     */
    @SaCheckPermission("event:message:remove")
    @Log(title = "聊天消息（按群组分区存储）", businessType = BusinessType.DELETE)
    @DeleteMapping("/{messageIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] messageIds) {
        return toAjax(chatMessageService.deleteWithValidByIds(List.of(messageIds), true));
    }


}
